import requests
import base64
from fontTools.ttLib import TTFont
from lxml import etree


def get_font(page):
    url = 'https://match.yuanrenxue.com/api/match/7?page=2'
    headers = {
        'User-Agent': 'yuanrenxue.project',
        'cookies': 'sessionid=s4i5zs85om37balue3mlnhhm2crjaj33'
    }
    params = {
        'page': str(page)
    }
    response = requests.get(url, headers=headers, params=params)
    response_json = response.json()
    # 将base64解码，保存成ttf
    font_file = base64.b64decode(response_json.get('woff'))
    with open('match_07.ttf', 'wb') as fp:
        fp.write(font_file)

    # 保存成xml格式
    font_obj = TTFont('match_07.ttf')
    font_obj.saveXML('match_07.xml')
    data_list=response_json.get('data')
    return data_list

def get_parse(data_list,page):
    data=etree.parse('match_07.xml')
    das=data.xpath('//ttFont/glyf/TTGlyph')
    dic=dict()
    for da in das:
        name=da.xpath('./@name')[0]
        on=''.join(da.xpath('.//contour/pt/@on'))
        dic[name]=on
    # print(dic)

    #   对data数据进行操作
    yyq = 1
    for i in data_list:
        values=i.get('value')
        value=values.split(' ')
        value = [i.replace('&#x','uni') for i in value if i != '']
        # print(value)
        # 将uni***映射成值
        value_list = ['0']*len(value)
        for v in range(len(value)):
            value_list[v]=dict_ys.get(dic.get(value[v]))

        all_sum.append(''.join(value_list))
        all_name.append(names[yyq+(page-1)*10])
        yyq+=1



if __name__ == '__main__':
    dict_ys={'111':'.','10010101001110101011010101010101000100100':'9','10101100101000111100010101011010100101010100':'3',
         '1001101111':'1','101010101101010001010101101010101010010010010101001000010':'8','100110101001010101011110101000':'2',
         '1110101001001010110101010100101011111':'5','10101010100001010111010101101010010101000':'6','111111111111111':'4',
         '1111111':'7','10100100100101010010010010':'0'}
    names=[
    "极镀ギ紬荕",
    "爷灬霸气傀儡",
    "梦战苍穹",
    "傲世哥",
    "мaη肆風聲",
    "一刀メ隔世",
    "横刀メ绝杀",
    "Q不死你R死你",
    "魔帝殤邪",
    "封刀不再战",
    "倾城孤狼",
    "戎马江湖",
    "狂得像风",
    "影之哀伤",
    "謸氕づ独尊",
    "傲视狂杀",
    "追风之梦",
    "枭雄在世",
    "傲视之巅",
    "黑夜刺客",
    "占你心为王",
    "爷来取你狗命",
    "御风踏血",
    "凫矢暮城",
    "孤影メ残刀",
    "野区霸王",
    "噬血啸月",
    "风逝无迹",
    "帅的睡不着",
    "血色杀戮者",
    "冷视天下",
    "帅出新高度",
    "風狆瑬蒗",
    "灵魂禁锢",
    "ヤ地狱篮枫ゞ",
    "溅血メ破天",
    "剑尊メ杀戮",
    "塞外う飛龍",
    "哥‘K纯帅",
    "逆風祈雨",
    "恣意踏江山",
    "望断、天涯路",
    "地獄惡灵",
    "疯狂メ孽杀",
    "寂月灭影",
    "骚年霸称帝王",
    "狂杀メ无赦",
    "死灵的哀伤",
    "撩妹界扛把子",
    "霸刀☆藐视天下",
    "潇洒又能打",
    "狂卩龙灬巅丷峰",
    "羁旅天涯.",
    "南宫沐风",
    "风恋绝尘",
    "剑下孤魂",
    "一蓑烟雨",
    "领域★倾战",
    "威龙丶断魂神狙",
    "辉煌战绩",
    "屎来运赚",
    "伱、Bu够档次",
    "九音引魂箫",
    "骨子里的傲气",
    "霸海断长空",
    "没枪也很狂",
    "死魂★之灵"
]
    all_sum = []
    all_name=[]

    for page in range(1, 6):
        res = get_font(page)
        datas=get_parse(res,page)
    # print(all_sum)
    # print(all_name)
    des=dict()
    for i,j in zip(all_name,all_sum):
        des[i]=int(j)
    print(des)
    print(sorted(des.items(),key=lambda x:x[1],reverse=True))


